package com.longrise.android.byjk.plugins.tabthird.invoice;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.widget.view.BBswipeRefreshLayout;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;


/**
 * Created by Administrator on 2017/10/22.
 * 开票历史详情
 */

public class BillDetailActivity extends BaseActivity2 implements SwipeRefreshLayout.OnRefreshListener {

    private TextView mExpressName;
    private TextView mExpressNum;
    private TextView mHeaderInfo;
    private TextView mInvoiceType;
    private TextView mInvoiceHeader;
    private TextView mInvoiceNum;
    private TextView mInvoiceContent;
    private TextView mTaxpayerType;
    private TextView mInvoiceMount;
    private TextView mAddressee;
    private TextView mTelNum;
    private TextView mReceiveAdd;
    private TextView tv_bill_detail_notice;
    private RecyclerView mCostDetailRcv;
    private BillDetailRcvAdapter mRcvAdapter;

    public static final String INVOICE_STATE = "invoiceState";


    public static final String INVOICE_ID = "invoiceid";
    private LinearLayout ll_bill_notice;
    private LinearLayout ll_bill_mail;
    private LinearLayout ll_bill_num;
    private View header_view1;

    //开票历史的id
    private String mBillId;

    private EntityBean mBillDetailbeans;
    //费用详情的bean
    private EntityBean[] mAccountDetailbeans;
    //快递类型
    private String expressName;

    private BBswipeRefreshLayout mSwipeRefreshLayout;

    private LinearLayout mHeaderView = null;

    //抬头类型
    private String headerType;

    //发票类型
    private String invtype;

    //发票内容
    private String contentType;

    private int flags;


    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_bill_detail;
    }

    @Override
    public void initView() {
        mSwipeRefreshLayout = (BBswipeRefreshLayout)findViewById(R.id.home_fragment_swipelayout);
        mCostDetailRcv = (RecyclerView) findViewById(R.id.rcv_cost_detail);

        //头部header
        mHeaderView = (LinearLayout) View.inflate(mContext, R.layout.header_bill_detail_view, null);

        mExpressName = (TextView)mHeaderView.findViewById(R.id.tv_express_name);
        mExpressNum = (TextView) mHeaderView.findViewById(R.id.tv_express_num);
        mHeaderInfo = (TextView) mHeaderView.findViewById(R.id.tv_header_info);
        mInvoiceType = (TextView) mHeaderView.findViewById(R.id.tv_invoice_type);
        mInvoiceHeader = (TextView) mHeaderView.findViewById(R.id.tv_invoice_header);
        mInvoiceNum = (TextView) mHeaderView.findViewById(R.id.tv_invoice_num);
        mInvoiceContent = (TextView) mHeaderView.findViewById(R.id.tv_invoice_content);
        mTaxpayerType = (TextView) mHeaderView.findViewById(R.id.tv_taxpayer_type);
        mInvoiceMount = (TextView) mHeaderView.findViewById(R.id.tv_invoice_mount);
        mAddressee = (TextView) mHeaderView.findViewById(R.id.tv_addressee);
        mTelNum = (TextView) mHeaderView.findViewById(R.id.tv_tel_num);
        mReceiveAdd = (TextView) mHeaderView.findViewById(R.id.tv_receive_address);
        ll_bill_notice = (LinearLayout) mHeaderView.findViewById(R.id.ll_bill_notice);
        ll_bill_mail = (LinearLayout) mHeaderView.findViewById(R.id.ll_bill_mail);
        ll_bill_num = (LinearLayout) mHeaderView.findViewById(R.id.ll_bill_num);
        header_view1 = mHeaderView.findViewById(R.id.header_view1);
        tv_bill_detail_notice = (TextView) mHeaderView.findViewById(R.id.tv_bill_detail_notice);
        getExtraData();
        initRcv();
        clientRequest();
        setToolbarTitle(AppUtil.getString(R.string.bill_history_detail));

    }

    public void setToolbarTitle(String title) {
        mToolbar.setTitle(title);
    }

    @Override
    public void onToolbarBackClick() {
        switch (flags){
            case InvoiceConfirmActivity.INVOICE_CONFIRM_ACTIVITY:
                startActivity(InvoiceManagerActivity.class);
                break;

            case BillHistoryActivity.BILL_HISTORY_ACTIVITY:
                finish();
                break;
        }
    }

    private void initRcv(){
        mSwipeRefreshLayout.setOnRefreshListener(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mCostDetailRcv.setLayoutManager(linearLayoutManager);
        mRcvAdapter = new BillDetailRcvAdapter();
        mRcvAdapter.setHeaderView(mHeaderView);
        mCostDetailRcv.setAdapter(mRcvAdapter);
    }

    private void getExtraData() {
        Intent intent = getIntent();
        flags = intent.getFlags();
        mBillId = intent.getStringExtra(INVOICE_ID);

        switch (flags){
            case InvoiceConfirmActivity.INVOICE_CONFIRM_ACTIVITY:
                setToolbarBackIcon(R.drawable.by_ic_close_black);
                break;

            case BillHistoryActivity.BILL_HISTORY_ACTIVITY:
                setToolbarBackIcon(R.drawable.ic_back_main);
                break;
        }

    }

    private void clientRequest() {
        EntityBean bean = new EntityBean();
        bean.set("id", mBillId);
        EntityBean bean1 = new EntityBean();
        bean1.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "searchPreinvoiceByID", bean1, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    parse(result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onFinished(String key, String service) {
                mSwipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

        });
    }

    private void parse(Object result) {

        EntityBean bean = (EntityBean) result;
        mBillDetailbeans = (EntityBean) bean.get("result");
        mAccountDetailbeans = (EntityBean[]) mBillDetailbeans.get("chargebeans");
        mRcvAdapter.refreshData(mAccountDetailbeans);
        setData2View(mBillDetailbeans);
    }

    public void setData2View(EntityBean bean){

        String mBillState= bean.get("invstate").toString();
        String customertype= bean.get("customertype").toString();
        String invtype= bean.get("invtype").toString();
        String customername= bean.get("customername").toString();
        String invoiceitem= bean.get("invoiceitem").toString();
        String taxpayertype= bean.get("taxpayertype").toString();
        String amt= bean.get("amt").toString();
        String addressee= bean.get("addressee").toString();
        String mobile= bean.get("mobile").toString();
        String sendaddress= bean.get("sendaddress").toString();

        //处理中
        if(mBillState.equals("1")){
            ll_bill_notice.setBackgroundColor(Color.parseColor("#FFFAEDD4"));
            tv_bill_detail_notice.setText(AppUtil.getString(R.string.bill_in_progres));
            tv_bill_detail_notice.setTextColor(Color.parseColor("#FFFBA140"));
            ll_bill_mail.setVisibility(View.GONE);
            ll_bill_num.setVisibility(View.GONE);
            header_view1.setVisibility(View.GONE);
        }else if(mBillState.equals("2")){
            ll_bill_notice.setBackgroundColor(Color.parseColor("#FF1BA6FF"));
            tv_bill_detail_notice.setText(AppUtil.getString(R.string.bill_mail));
            tv_bill_detail_notice.setTextColor(Color.parseColor("#FFFFFFFF"));
            ll_bill_mail.setVisibility(View.VISIBLE);
            ll_bill_num.setVisibility(View.VISIBLE);
            header_view1.setVisibility(View.VISIBLE);
            String trackingname= bean.get("trackingname").toString();
            String trackingnum= bean.get("trackingnumber").toString();
            String billno= bean.get("billno").toString();
            mExpressName.setText(getTrackName(trackingname));
            mExpressNum.setText(trackingnum);
            mInvoiceNum.setText(billno);
        }

//        mExpressName.setText(trackingname);
//        mExpressNum.setText(trackingnum);
        mHeaderInfo.setText(customertype);
        mInvoiceType.setText(invtype);
        mInvoiceHeader.setText(customername);
        mInvoiceContent.setText(invoiceitem);
        mTaxpayerType.setText(taxpayertype);
        mInvoiceMount.setText(amt);
        mAddressee.setText(addressee);
        mTelNum.setText(mobile);
        mReceiveAdd.setText(sendaddress);
    }

    public String getTrackName(String type){
        if("1".equals(type)){
            expressName = AppUtil.getString(R.string.ems);
        }else if("2".equals(type)){
            expressName = AppUtil.getString(R.string.shunfeng);
        }else if("3".equals(type)){
            expressName = AppUtil.getString(R.string.yuantong);
        }else if("4".equals(type)){
            expressName = AppUtil.getString(R.string.yunda);
        }else if("5".equals(type)){
            expressName = AppUtil.getString(R.string.zhongtong);
        }else if("6".equals(type)){
            expressName = AppUtil.getString(R.string.anneng);
        }

        return expressName;
    }

    @Override
    public void onRefresh() {
        clientRequest();
    }

    //转换抬头类型
    public String getCustomType(String type){
        if("0".equals(type)){
            headerType = AppUtil.getString(R.string.heder_type_person);
        }else if("1".equals(type)){
            headerType = AppUtil.getString(R.string.heder_type_company);
        }
        return headerType;
    }

    //转换发票类型
    public String getInvtype(String type){
        if("0".equals(type)){
            invtype = AppUtil.getString(R.string.invoice_type_normal);
        }else if("1".equals(type)){
            invtype = AppUtil.getString(R.string.invoice_type_special);
        }
        return invtype;
    }

    //转换发票内容
    public String getInvoiceitem(String type){
        if("1".equals(type)){
            contentType = AppUtil.getString(R.string.invoice_content_service);
        }else if("2".equals(type)){
            contentType = AppUtil.getString(R.string.invoice_content_platform);
        }else if("3".equals(type)){
            contentType = AppUtil.getString(R.string.invoice_content_course);
        }
        return contentType;
    }

    //转换纳税人类型
    public String getTaxpayertype(String type){
        if("0".equals(type)){
            invtype = AppUtil.getString(R.string.taxpayer_type_normal);
        }
        return invtype;
    }

}
